// InitState.cpp: implementation of the InitState class.
//
//////////////////////////////////////////////////////////////////////

#include "InitState.h"

#ifdef FREEGLUT_IS_PRESENT
#  include <GL/freeglut.h>
#else
#  ifdef __APPLE__
#    include <GLUT/glut.h>
#  else
#    include <GL/glut.h>
#  endif
#endif

#include <plib/js.h>
#include <plib/ssg.h>

#include "ui.h"

#include "TrailMapEditor.h"
#include "Sector.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

InitState::InitState()
: text1(0)
, valuator1(0)
{        
    this->load("InitState.xml");
}

InitState::~InitState()
{
    delete scene;
}

void InitState::load(const char* filename)
{
	scene = new uiScene(filename);
	scene->setDelegate(this);
	
	uiSynthesize(scene, puText, text1)
	uiSynthesize(scene, puText, text2)
	if(text1) { this->text1->setValuator(&valuator1); }
    uiSynthesize(scene, puInput, input2)
    uiSynthesize(scene, puDial, dial1)
    uiSynthesize(scene, puPopup, popup1)
    uiSynthesize(scene, puDialogBox, dialogBox1)
    uiSynthesize(scene, uiButton, treesButton)
    uiSynthesize(scene, puPopupMenu, popupMenu1)
    uiSynthesize(scene, puButton, exitButton2)
	uiSynthesize(scene, puOneShot, submenuitem_save)
	uiSynthesize(scene, puOneShot, vsubmenuitem_open)
    uiSynthesize(scene, uiListBox, listBox1);
    uiSynthesize(scene, uiButtonBox, buttonBox1);
    uiSynthesize(scene, uiComboBox, comboBox1);
    uiSynthesize(scene, uiSelectBox, selectBox1);
    uiSynthesize(scene, uiFileSelector, fileSelector1);
}

void InitState::display(void)
{
    scene->display();
}

void InitState::resize(int w, int h)
{
    scene->resize(w, h);
}

void InitState::keyUp(unsigned char key, int x, int y)
{
	scene->keyUp(key, x, y);
}

void InitState::keyDown(unsigned char key, int x, int y)
{
	scene->keyDown(key, x, y);
}

void InitState::specialUp(int key, int x, int y)
{
	scene->specialUp(key, x, y);
}

void InitState::specialDown(int key, int x, int y)
{
	scene->specialDown(key, x, y);
}

void InitState::motion(int newx, int newy)
{
	scene->motion(newx, newy);
}

void InitState::passive(int newx, int newy)
{
	scene->passive(newx, newy);
}

void InitState::mouse(int button, int state, int x, int y)
{
	scene->mouse(button,state,x,y);
}

State* InitState::idle(void)
{
    return this;//(State *)new IntroState();
}

// uiSceneDelegate
void InitState::onCallback(const uiScene* scene, const uiView* view, const char* name, puObject* object)
{
	printf("onCallback(%p - %p,\"%s\",%s)\n",scene, view, name, object->getTypeString());
	if(ulStrEqual(name, "exitButton"))
	{
        uiApplication::getInstance().shutdown();
//		TrailMapEditor* app = (TrailMapEditor*)uiApplication::getInstancePtr();
//        app->shutdown();
	}
	else if(ulStrEqual(name, "dial1"))
	{
		puDial* dial = (puDial*)object;
		
		if(text1)
		{
			text1->setLabel(dial->getStringValue());
		}
	}
	else if(object == input2)
	{
		valuator1 = input2->getFloatValue();
		dial1->setValue(input2->getFloatValue());
	}
    else if(object == treesButton)
    {
        if(popup1->isVisible())
            popup1->hide();
        else
            popup1->reveal();
        
//        if(dialogBox1->isVisible())
//            dialogBox1->hide();
//        else
//            dialogBox1->reveal();
    }
    else if(object == dialogBoxOk)
    {
        dialogBox1->hide();
    }
    else if(object == exitButton2)
    {
        if(popupMenu1->isVisible())
            popupMenu1->hide();
        else
            popupMenu1->reveal();
    }
	else if(object == submenuitem_save)
	{
		if(text2)
		{
			text2->setLabel(object->getLegend());
		}
	}
	else if(object == vsubmenuitem_open)
	{
		if(text2)
		{
			text2->setLabel(object->getLegend());
		}
	}
    else if(object == listBox1)
    {
        uiListBox* listBox = (uiListBox*)object;
    }
    else if(object == buttonBox1)
    {
        uiButtonBox* buttonBox = (uiButtonBox*)object;
    }
    else if(object == comboBox1)
    {
        uiComboBox* comboBox = (uiComboBox*)object;
        
        printf("Combo Box : %s",comboBox->getStringValue());
    }
    else if(object == selectBox1)
    {
        uiSelectBox* selectBox = (uiSelectBox*)object;
        
        printf("Select Box : %s",selectBox->getStringValue());
    }
    else if(object == fileSelector1)
    {
        uiFileSelector* fileSelector = (uiFileSelector*)object;
        
        printf("File Selector : %s",fileSelector->getStringValue());
        
        Sector* sector = Sector::loadFromDEM(fileSelector->getStringValue());
        sector->print();
        
        scene->getRoot()->addKid(sector);
        //delete sector;
    }
}

void InitState::onActiveCallback(const uiScene* scene, const uiView* view, const char* name, puObject* object)
{	
	printf("onActiveCallback(%p - %p,\"%s\",%s)\n",scene, view, name, object->getTypeString());
}

void InitState::onDownCallback(const uiScene* scene, const uiView* view, const char* name, puObject* object)
{	
	printf("onDownCallback(%p - %p,\"%s\",%s)\n",scene, view, name, object->getTypeString());
    
    if(object == input2)
	{
		valuator1 = input2->getFloatValue();
		dial1->setValue(input2->getFloatValue());
	}
}


